package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.model.util.FreeCarryModeConvert;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.FreeCarryModeDtoMapper;
import com.starlink.model.domain.FreeCarryModeDO;
import com.starlink.mapper.FreeCarryModeMapper;
import com.starlink.model.request.FreeCarryModeRequest;
import com.starlink.service.FreeCarryModeService;
import com.starlink.model.vo.FreeCarryModeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class FreeCarryModeServiceImpl extends ServiceImpl<FreeCarryModeMapper, FreeCarryModeDO> implements FreeCarryModeService {
    @Autowired
    private FreeCarryModeMapper freeCarryModeMapper;

    @Override
    public FreeCarryModeVO getByKey(long id) {
        FreeCarryModeVO freeCarryModeVO = FreeCarryModeDtoMapper.INSTANCE.do2VO(getById(id));
        return freeCarryModeVO;
    }

    @Override
    public PageInfo<FreeCarryModeVO> pageQuery(Pageable pageable,FreeCarryModeRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<FreeCarryModeDO> list = freeCarryModeMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<FreeCarryModeVO> freeCarryModeVOList = FreeCarryModeConvert.buildFreeCarryModeVoList(list);
        return new PageInfo(freeCarryModeVOList);
    }

    @Override
    public List<FreeCarryModeVO> listQuery(FreeCarryModeRequest request) {
        List<FreeCarryModeDO> list = freeCarryModeMapper.selectDOList(FreeCarryModeDtoMapper.INSTANCE.request2DO(request));
        List<FreeCarryModeVO> freeCarryModeVOS = FreeCarryModeConvert.buildFreeCarryModeVoList(list);
        return freeCarryModeVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(FreeCarryModeRequest request) {
        save(FreeCarryModeDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(FreeCarryModeRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(FreeCarryModeDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        freeCarryModeMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsert(List<FreeCarryModeDO> carryModeDOS) {
        freeCarryModeMapper.batchInsertDO(carryModeDOS);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletedByTempId(Long templateId) {
        freeCarryModeMapper.deletedByTempId(templateId);
    }
}
